package com.shuorikeji.huohuo.ui.home;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.material.imageview.ShapeableImageView;
import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.PlAdapter;
import com.shuorikeji.huohuo.adapter.TagAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.BookDetailInfoSubBean;
import com.shuorikeji.huohuo.bean.BookDetailMainBean;
import com.shuorikeji.huohuo.bean.BookPlInfoBean;
import com.shuorikeji.huohuo.bean.BookPlMainBean;
import com.shuorikeji.huohuo.bean.UserInfoBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.common.Constant;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.BusinessUtils;
import com.shuorikeji.huohuo.utils.DialogUtil;
import com.shuorikeji.huohuo.utils.JsonUtil;
import com.shuorikeji.huohuo.utils.SpacesItemDecoration;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.shuorikeji.huohuo.view.GridDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class BookInfoActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.siv_fm)
    ShapeableImageView sivFm;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.tv_pf)
    TextView tvPf;
    @BindView(R.id.ll_pl)
    LinearLayout llPl;
    @BindView(R.id.tv_pl_count)
    TextView tvPlCount;
    @BindView(R.id.tv_read_rs)
    TextView tvReadRs;
    @BindView(R.id.rv_tag)
    RecyclerView rvTag;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.tv_desc_more)
    TextView tvDescMore;
    @BindView(R.id.ll_more_pl)
    LinearLayout llMorePl;
    @BindView(R.id.rv_pl)
    RecyclerView rvPl;
    @BindView(R.id.tv_go_read)
    TextView tvGoRead;

    private int BOOK_ID;
    private BookDetailInfoSubBean bookInfoDta;
    private TagAdapter tagAdapter;
    private List<String> tagList = new ArrayList<>();
    private List<BookPlInfoBean> plList = new ArrayList<>();//评论数据
    private PlAdapter plAdapter;
    private Dialog bookJjDialog;
    private int plTotalCount;//评论总数
    private UserInfoBean userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_book_info);
    }

    @Override
    protected void initData() {
        userInfo = BusinessUtils.getUserInfo();
        initAdapter();
        initIntent();
        initClick();
    }

    private void initClick() {
        tvDescMore.setOnClickListener(view -> {
            if (bookInfoDta == null) {
                ToastShowUtil.showToastCenter(BookInfoActivity.this, "无数据");
            } else {
                showBookJjDialog();
            }
        });
        llPl.setOnClickListener(view -> {
            skipToPlPage();
        });
        llMorePl.setOnClickListener(view -> {
            skipToPlPage();
        });
        sivFm.setOnClickListener(view -> {
            Intent intent = new Intent(BookInfoActivity.this, BookDetailActivity.class);
            intent.putExtra("BOOK_ID", BOOK_ID);
            startActivity(intent);
        });
        tvGoRead.setOnClickListener(view -> {
            Intent intent = new Intent(BookInfoActivity.this, BookDetailActivity.class);
            intent.putExtra("BOOK_ID", BOOK_ID);
            startActivity(intent);
        });
    }

    /**
     * 到评论页
     */
    private void skipToPlPage() {
        Intent intent = new Intent(BookInfoActivity.this, BookPlActivity.class);
        intent.putExtra("BOOK_INFO", JsonUtil.toJson(bookInfoDta));
        intent.putExtra("PL_COUNT", plTotalCount);
        startActivity(intent);
    }

    private void showBookJjDialog() {
        bookJjDialog = DialogUtil.showBookJj(BookInfoActivity.this, bookInfoDta.getContent(), new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {

            }
        });
    }

    private void initAdapter() {
        FlexboxLayoutManager manager = new FlexboxLayoutManager(this);
        manager.setFlexDirection(FlexDirection.ROW);
        manager.setFlexWrap(FlexWrap.WRAP);
        rvTag.addItemDecoration(new GridDividerItemDecoration(20));
        rvTag.setLayoutManager(manager);
        tagAdapter = new TagAdapter(BookInfoActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {

            }
        });
        rvTag.setAdapter(tagAdapter);

        rvPl.setLayoutManager(new LinearLayoutManager(BookInfoActivity.this, LinearLayoutManager.VERTICAL, false));
        rvPl.addItemDecoration(new SpacesItemDecoration(0, 0, 10, 0));
        plAdapter = new PlAdapter(BookInfoActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 2:
                        BookPlInfoBean data = (BookPlInfoBean) object;
                        if (userInfo != null) {

                        }
                        break;
                }
            }
        });
        rvPl.setAdapter(plAdapter);
    }

    private void initIntent() {
        Intent intent = getIntent();
        BOOK_ID = intent.getIntExtra("BOOK_ID", -1);
        getBookPl();
    }

    private void getBookPl() {
        mPresenter.getBookPl(BookInfoActivity.this, 1, 1, BOOK_ID, 10, 1, 1, true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        BookPlMainBean data = (BookPlMainBean) object;
                        plList.clear();
                        plList.addAll(data.getData().getData());
                        plAdapter.setData(plList);
                        plTotalCount = data.getData().getTotal();
                        tvPlCount.setText(plTotalCount + " 人点评");
                        getBookDetail();
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(BookInfoActivity.this, msg);
                        getBookDetail();
                        break;
                }
            }
        });
    }

    private void getBookDetail() {
        mPresenter.getBookDetail(BookInfoActivity.this, BOOK_ID, true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        BookDetailMainBean data = (BookDetailMainBean) object;
                        bookInfoDta = data.getData();
                        if (bookInfoDta == null) {
                            ToastShowUtil.showToastCenter(BookInfoActivity.this, "无数据");
                        } else {
                            tagList.clear();
                            tagList.addAll(bookInfoDta.getTag_id());
                            tagAdapter.setData(tagList);
                            showBookInfo();
                        }
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(BookInfoActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void showBookInfo() {
        BusinessUtils.setLoadImg(BookInfoActivity.this, Constant.IMG_OSS_BASE_URL + bookInfoDta.getCover_image(), sivFm);
        tvName.setText(bookInfoDta.getName());
        tvTips.setText(bookInfoDta.getAuthor() + " · " +
                ((bookInfoDta.getPublish_status() == 1) ? "连载中" : "已完结")
                + " · " + bookInfoDta.getWord_num() + "万字数");
        tvPf.setText(bookInfoDta.getScore());
        tvReadRs.setText(String.valueOf(bookInfoDta.getReading_num()));
        tvDesc.setText("简介:" + Html.fromHtml(bookInfoDta.getContent()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BusinessUtils.clearDialog(bookJjDialog);
    }
}